package servlets;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.JasperDesign;

import java.sql.*;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLException;
import java.sql.Statement;


import javax.servlet.ServletOutputStream;


import net.sf.jasperreports.engine.JRResultSetDataSource;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
@WebServlet(name="listeAllThemesPDF", urlPatterns={"/JSP/Christine/listeAllThemesPDF"})



public class listeAllThemesPDF extends HttpServlet  {
	  /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
	 * @throws JRException 
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws SQLException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, JRException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
         ServletOutputStream servletOutputStream = response.getOutputStream();
    byte[] bytes = null;

    
    try{
    
    		
    		String connectionURL = "jdbc:mysql://localhost:3306/dbsurvey"; 
    	   
    	    // declare a connection by using Connection interface 
    	    Connection connection = null; 

    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			connection = (Connection) DriverManager.getConnection(connectionURL, "root", "root");
    		
    			// Recuperer la liste de tous les themes de la base de donnees "dbsurvey"
    			PreparedStatement ps = (PreparedStatement) connection.prepareStatement("SELECT * FROM `Theme` ");
    		    ResultSet rs = (ResultSet) ps.executeQuery();
 	
    	
    		    JasperPrint jasperPrint; 
 
    		    // Recuperer le fichier "ListeThemes.jrxml" qui continent la definition du rapport "Liste Tous les Themes"
    		    String str = request.getRealPath("/WEB-INF/reports/ListThemes.jrxml");
    		    File reportFile = new File(str);
    		    
    		    //Charger le fichier JRXml d'apres la classe JRXML Loader 
    		    JasperDesign jasperDesign = JRXmlLoader.load(reportFile);


    		    //Compiler le rapport JRXML en un fichier Jasper
    		    JasperReport	jasperreport = JasperCompileManager.compileReport(jasperDesign); 
    
    		    
    		    JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
	    
    		    // remplisser le rapport "ListeThemes" par les themes recuperes de la base de donnees "dbsurvey"
    		    jasperPrint     = JasperFillManager.fillReport(jasperreport, null, resultSetDataSource);
	    
    		    
    		    //JasperExportManager.exportReportToPdfFile(jasperPrint,"report.pdf");

    		    // new JasperPDFExporter(response, rs).BuidReport("C:/Users/christy/workspace/QID/WebContent/WEB-INF/classes/listeThemes.jrxml");

    		    
	    //exporter le rapport en format PDF
	    bytes = JasperRunManager.runReportToPdf(jasperreport, null,resultSetDataSource);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
	  
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
    
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
  	
    }

    
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
			processRequest(request, response);
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    
    }
    
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
			processRequest(request, response);
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>


    
    }

